package integracion;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import dominio.*;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ControlClienteCompania {
    private SqlMapClient sqlMap;

    public ControlClienteCompania() throws IOException, SQLException {

        String resource = "configuracion/sql-map-config.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        this.sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
    }

    public boolean agregarClienteCompania(ClienteCompania compania) {

        try {
            sqlMap.insert("agregarClienteCompania",compania);
            return true;

        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public List<ClienteCompania> consultarClienteCompania(){

        try{

            List compania = sqlMap.queryForList("consultarClienteCompania");
            return compania;

        } catch (SQLException e){
            Logger.getLogger(ClienteCompania.class.getName()).log(Level.SEVERE,null,e);
            return null;
        }

    }

    public List<ClienteCompania> consultarClienteCompaniaSegundaCuota(){

        try{

            List compania = sqlMap.queryForList("consultarClienteCompaniaSegundaCuota");
            return compania;

        } catch (SQLException e){
            Logger.getLogger(ClienteCompania.class.getName()).log(Level.SEVERE,null,e);
            return null;
        }

    }

    public List<ClienteCompania> consultarClienteCompaniaSinPagar(){

        try{

            List compania = sqlMap.queryForList("consultarClienteCompaniaSinPagar");
            return compania;

        } catch (SQLException e){
            Logger.getLogger(ClienteCompania.class.getName()).log(Level.SEVERE,null,e);
            return null;
        }

    }

    public List<ClienteCompania> consultarClienteCompaniaSinCuotas(){

        try{

            List compania = sqlMap.queryForList("consultarClienteCompaniaSinCuotas");
            return compania;

        } catch (SQLException e){
            Logger.getLogger(ClienteCompania.class.getName()).log(Level.SEVERE,null,e);
            return null;
        }

    }

    public List<ClienteCompania> consultarClientesCompEventosAnuales(Integer anio){

        try{

            List companias = sqlMap.queryForList("consultarClientesCompEventosAnuales",anio);
            return companias;

        } catch (SQLException e){
            Logger.getLogger(ClienteCompania.class.getName()).log(Level.SEVERE,null,e);
            return null;
        }

    }

    public boolean eliminarClienteCompania(ClienteCompania compania) {

        try {
            sqlMap.delete("eliminarClienteCompania",compania);
            return true;

        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean modificarClienteCompania(ClienteCompania compania) {

        try {
            sqlMap.update("modificarClienteCompania",compania);
            return true;

        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public SqlMapClient getSqlMap() {
        return sqlMap;
    }

    public void setSqlMap(SqlMapClient sqlMap) {
        this.sqlMap = sqlMap;
    }
}